const app = getApp();
Page({
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    //深色
    bottomColor: '#4ebfcc',
    //浅色
    gradientColor:'#42c7bb',

    scroll_left:'0rpx',

    arrCode:[],
    curtItem:null,
    deviceCode:null,

    alarmFastBreath:0,     // 呼吸过快报警
    alarmFastHeart:0,      // 心跳过快报警
    alarmNoHeart:0,        // 没有心跳多久报警
    alarmOpen:0,           // 报警开关
    alarmSlowBreath:0,     // 呼吸过慢报警
    alarmSlowHeart:0,      // 心跳过慢报警
    alarmPhone:0           // 设置报警电话
  },

  //删除
  del:function(event){
    let that = this;
    let deviceCode = event.target.dataset.item.deviceCode;
    wx.request({
      url: app.globalData.url+'deleteUserDeivce/'+deviceCode,
      data:{},
      method:'POST',
      header:{
        token:app.globalData.token
      },
      success:function(res){
        console.log("删除成功=====",res)
        that.getDeviceList();
      }
    })
  },

  //配置
  done: function (event) {
    this.setData({
      scroll_left: '0rpx'
    });
    let config = event.target.dataset.item;
    console.log("配置====",'心快==',config.alarmFastHeart,'心慢',config.alarmSlowHeart,'呼快',config.alarmFastBreath,'呼慢',config.alarmSlowBreath,'开关',config.alarmOpen,'无心跳',config.alarmNoHeart)
    // 跳转设备设置页面
    wx.navigateTo({
      url: '../device/device?deviceName='+config.deviceName+'&alarmFastBreath='+config.alarmFastBreath+'&alarmFastHeart='+config.alarmFastHeart+'&alarmNoHeart='+config.alarmNoHeart+'&alarmOpen='+config.alarmOpen+'&alarmSlowBreath='+config.alarmSlowBreath+'&alarmSlowHeart='+config.alarmSlowHeart+'&alarmPhone='+config.alarmPhone+'&deviceCode='+config.deviceCode,
    })

  },


  // 获取设备列表
  getDeviceList(){
    let that = this;
    wx.request({
      url: app.globalData.url+'getUserDevice',
      data:{},
      method:'GET',
      header:{
        token:app.globalData.token
      },
      success:function(res){
        console.log("获取用户绑定设备=======",res);
        if(res.data.code == 0){
          res.data.data.map((item,index)=>{
            item.inputShowed = false;
            if(item.userDeviceName!==null){
              item.deviceName = item.userDeviceName
            }else{
              item.deviceName = '设备'+index;
            }
            item.isRame = false;
          })
          that.setData({
            arrCode:res.data.data,
            scroll_left: '0rpx'
          })
        }else{
          that.setData({
            arrCode:[],
            scroll_left: '0rpx'
          })
        }
      }
    })
  },

  //设备重命名
  getRame(e){
    let that = this;
    // console.log("重新命名===",e.target.dataset.item)
    e.target.dataset.item.inputShowed = true;
    this.data.arrCode.map((item,index)=>{
      if(e.target.dataset.item.id == item.id){
        that.setData({
          ['arrCode['+index+']']:e.target.dataset.item
        })
      }
    })
  },

  


  // 绑定设备
  bindDevice(){
    this.getDeviceCode();
  },
  bindDevice1(){
    this.getDeviceCode1();
  },
  // 扫描二维码
  getDeviceCode(){
    let that = this;
    wx.scanCode({
      success (res) {
        console.log('扫描二维码=====',res);
        wx.request({
          url: app.globalData.url+'bindDevice/'+res.result,
          data:{
          },
          method:'POST',
          header:{
            token:app.globalData.token
          },
          success:function(r){
            console.log("提示结果。。。。。",r)
            if(r.data.code == 0){
              wx.showModal({
                title:'提示',
                content:r.data.msg,
                showCancel:false,
                success(r){
                  if(r.confirm){
                    // 确定
                    //绑定成功获取设备列表
                    that.getDeviceList();
                    
                  }else if(r.cancel){
                    // 取消
                  }

                }
              })
            }
          },
          fail:function(r){
            console.log("失败======",r)
          }
        })

      }
    })
  },

  getDeviceCode1(){
    let that = this;
    wx.request({
      url: app.globalData.url+'bindDevice/579851534952',
      data:{
      },
      method:'POST',
      header:{
        token:app.globalData.token
      },
      success:function(r){
        console.log("提示结果。。。。。",r)
        if(r.data.code == 0){
          wx.showModal({
            title:'提示',
            content:r.data.msg,
            showCancel:false,
            success(r){
              if(r.confirm){
                // 确定
                //绑定成功获取设备列表
                that.getDeviceList();
                
              }else if(r.cancel){
                // 取消
              }

            }
          })
        }
      },
      fail:function(r){
        console.log("失败======",r)
      }
    })
  },
  onLoad: function (options) {
    this.getDeviceList();
    this.setData({
      deviceCode:app.globalData.deviceCode
    })
  },


  onReady: function () {
    
  },

  onShow: function () {
    this.getDeviceList();
    this.setData({
      deviceCode:app.globalData.deviceCode
    })

  },

  onHide: function () {
    
  },

 
  onUnload: function () {
    
  },


  onPullDownRefresh: function () {
    
  },


  onReachBottom: function () {
    
  },

  onShareAppMessage: function () {
    
  }


})